<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\controller\user\server\Apply;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\model\server\User as ServerModel;

use app\common\service\Order as ServiceOrder;
/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        // 订单总数
        $model = new OrderModel;

        $data = [
            'userInfo' => $user,
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),
                'un_finish' => $model->getCount($user, 'un_finish'),
                'finish' => $model->getCount($user, 'finish'),
                'cancel' => $model->getCount($user, 'cancel'),
            ],
            'server_info'=>ServerModel::get(['user_id'=>$user['user_id']]),
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
            ],
            'menus' => (new UserModel)->getMenus()   // 个人中心菜单列表
        ];
        $data = collection($data)->toArray();
        if(!$data['server_info']){

            $res = \app\api\model\server\Apply::get(['user_id'=>$user['user_id'],'apply_status'=>10]);
            if($res){
                $data['menus']['server'] = [
                    'name' => '申请状态',
                    'url' => 'pages/server/sqserver',
                    'icon' => 'kanjia'
                ];
            }else{
                $data['menus']['server'] = [
                    'name' => '申请服务',
                    'url' => 'pages/server/shenqing',
                    'icon' => 'kanjia'
                ];
            }
        }

//        var_dump($user);
        if(!$user){
            unset($data['menus']['server']);
            unset($data['menus']['dealer']);
        }else{
            if($user['show_server'] == '不显示'){
                unset($data['menus']['server']);
            }
            if($user['show_dealer'] == '不显示'){
                unset($data['menus']['dealer']);
            }
        }

        return $this->renderSuccess($data);
    }
}
